/**
 *@name Ccay.enum.EditEnumItem
 *@description 编辑页面控制器
 *@param page 该参数为控制器对象的内部别名，方便控制器内部的调用。外部访问不到，外部只能通过Ccay.enum.EditEnumItem访问其方法或属性
 */
Ccay.Module.define("Ccay.enum.EditEnumItem",function(page,$S){
	/**
	 * 导入枚举类型
	 */
	Enum.import("SourceEnum");
	/**
	 * 初始化Grid
	 */
	page.ready = function(){
		var gridOptions={
			allowFreezen: true,
			editMode : true,
			showSeq: false,
			columns:[
		    {
		    	field: "type", 
			    width: 150, 
			    header: "ccay.enum.label.type",
			    type: 'enum',
			    format: {
			    	service:"REST:services/manage/enums/findAllEnumType",
			    	valueField:"key",
			    	displayField:"value"
			    },
			    rule : {
			    	required : true
			    }
			}, 
		    {
		    	field: "name", 
			    width: 150, 
			    header: "ccay.enum.label.name", 
			    editable:false
			}, 
			{
		    	field: "value", 
			    width: 100, 
			    header: "ccay.enum.label.value", 
			    rule : {
				    required: true, 
				    maxlength: 50
			    }
			},
			{
				field: "displayValue", 
				width: 100, 
				header: "ccay.enum.label.displayValue", 
				rule : {
					required: true, 
					maxlength: 50
				}
			},
			{
		    	field: "index", 
			    width: 100, 
			    header: "ccay.enum.label.index", 
			    rule : {
				    required: true, 
				    maxlength: 50,
				    digits : true
			    }
			},
			{
		    	field: "source", 
			    width: 100,
			    type:'enum',
			    format:{service: 'ENUM:SourceEnum'},
			    header: "ccay.enum.label.source", 
			    editable:false
			    
			},
			{
				field: "description", 
				width: 300, 
				header: "ccay.enum.label.description", 
				rule : {
					maxlength: 200,
				}
			}],
			fnBeforeEdit: function (value, td, col) {
				// 得到行对象
				var row = this.getCellRow(td);
				// 得到行数据
				var rowData = this.getRowData(row);
				// 控制单元格是否可编辑
				return rowData.source == Enum.SourceEnum.SOURCE_DB;
			},
			fnBeforeRequest: function (op) {
				if(!$S("input[name='index']").val()){
					op.ajax.data.index = -1; 
				}
			},
			ajax:{				
				url: 'services/manage/enums/findPagedEnums/{0}/{1}'
			},
		    operation:{
			   search: {
				   btn: '#btnEnumSearch',
				   panel: '#enumForm'
			   },
			   save:{
				   btn:'#btnSave',
				   ajax:{
					   url:'services/manage/enums/batch'
				   },
				   fnBeforeSave:function(params,ajax, grid, optOp){
		    			var result = true;
						Ccay.doPut({
				 			async:false,
				 			url:"services/manage/enums/validateForBatch",
				 			data : params,
				 			success:function(data){
				 				var message = [];
				 				//1、类、值唯一
				 				if(data.TypeValueUniqueFail){
				 					message.push("类、值 {0} 重复！".formatValue(data.TypeValueUniqueFail.join(",")));
				 					result = false;
				 				}
				 				//2、类、序号唯一
				 				if(data.TypeIndexUniqueFail){
				 					message.push("类、序号 {0} 重复！".formatValue(data.TypeIndexUniqueFail.join(",")));
				 					result = false;
				 				}
				 				if(!result){
				 					Ccay.UI.showMessage(message.join("</br>"),'error',null, false,false);
				 				}
				 			}			
				 		});
				 		return result;
		    		}
			   },
			   insert:{
				   btn:'#btnAdd',
		   		   defaultRow:{source:"Database"}
			   	},
			   del:{
				   btn:'#btnDelete',
				   undoBtn : '#btnUndoDel'
			   }
		   }
		}
		if (Ccay.Permission.check("enumManagement:delete")) {
			//有删除权限，渲染多选列
			var newColums = [
			   	{
					columnType: "select", 
				    multi: true, 
				    cssName: "center", 
				    freezeable: true
				}]; 
			gridOptions.columns = newColums.concat(gridOptions.columns);
			//有删除权限，控制只能删除数据库中的枚举类
			gridOptions.fnRenderRow=function(row,data,i){
				var select=row.find("td[eno=rowSelect]");
				if(data.source != "Database"){
					select.removeClass("grid-select");
					row.addClass("row-noselect");
					return;
				}
			}
		}
		$S("#enumItemGrid").igrid(gridOptions);
	};
	
   /**
	*lookup 导入
	*/
	page.lookupImport=function(){
		var param={uploadType:"ExcelImport",
				completeCallback: Ccay.Component.Excel.uploadCallback,
				closeDialog:true,
				query:"excelType=lookupItemImport"};
		 Ccay.Component.openUploadDialog($i18n("ccay.common.button.import"),param);
	} 
	
	/**
	*lookup 导出
	*/
	page.exportItemByIExcel=function(){
		var map=Ccay.Page.getRequest();
		var classifyCode=map["classifyCode"];
		Ccay.doGet("services/ccay/lookup/export/single/"+classifyCode,'');
	}
	
});




